package com.kc.sjwh.service;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kc.sjwh.dao.RyDao;
import com.kc.sjwh.entity.po.Ry;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.other.Tool;

/**
 * 人员 service
 * @author zhangguihua(hua7381@163.com)
 * date: 2018年2月22日
 */
@Service
public class RyService {
	static Logger logger = Logger.getLogger(RyService.class);
	@Autowired
	RyDao ryDao;

	public Page<Ry> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("cjsj desc, rybh");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			v = v.trim();
			if(!"".equals(v)){
				q.addSqlCondition("ryjh like ? or ryxm like ? or ssdwmc like ?".replace("?", "'%"+v+"%'"));
			}
		}
		v = param.get("dwdm");
		if(StringUtils.isNotEmpty(v)) {
			q.put("ssdwdm", v);
		}
		
		return ryDao.getPage("get", q);
	}

	public void add(Ry one) {
		one.setCjsj(new Date());
		one.setRybh(Tool.getUuid());
		ryDao.add(one);
	}

	public void update(Ry one) {
		one.setGxsj(new Date());
		ryDao.update(one, "rybh");
	}

	@Transactional
	public void delete(String id) {
		//级联删除 none
		
		//删除
		int n = ryDao.delete(id);
		if(n == 0) {
			throw new IllegalArgumentException("要删除的记录不存在");
		}
	}

	public Ry load4Update(String id) {
		return ryDao.getOne(new QueryParam().put("rybh", id));
	}
	
}
